import Vue from "vue";
import Router from "vue-router";
import { getItem, removeItem } from "@/utils/localStorageOpe";
import http from "@/request/http";

// import MyHome from "@/components/MyHome.vue";

Vue.use(Router);

const router = new Router({
    routes: [{
            path: "/",
            redirect: "/home",
            hidden: true, // 设置一个hidden属性
        },
        // {
        //     path: "/home",
        //     // component: MyHome
        //     component: (resolve) =>
        //         // import ("@/components/MyHome.vue"), // 路由的懒加载, 官方推荐写法
        //         require(["@/components/MyHome.vue"], resolve), // 路由的异步加载
        // },
        {
            path: "/login",
            component: () =>
                import ("@/components/Login.vue"),
            hidden: true,
        },
        {
            path: "/home",
            name: "学生管理",
            iconClass: "fa fa-users",
            redirect: "/home/studentList",
            component: () =>
                import ("@/components/MyHome.vue"),
            children: [{
                    path: "/home/studentList",
                    name: "学生列表",
                    iconClass: "fa fa-list",
                    component: () =>
                        import ("@/components/studentManager/StudentList.vue"),
                },
                {
                    path: "/home/infoList",
                    name: "信息列表",
                    iconClass: "fa fa-list",
                    component: () =>
                        import ("@/components/studentManager/InfoList.vue"),
                },
                {
                    path: "/home/infoManager",
                    name: "信息管理",
                    iconClass: "fa fa-list",
                    component: () =>
                        import ("@/components/studentManager/InfoManager.vue"),
                },
                {
                    path: "/home/workList",
                    name: "作业列表",
                    iconClass: "fa fa-list",
                    component: () =>
                        import ("@/components/studentManager/WorkList.vue"),
                },
                {
                    path: "/home/workManager",
                    name: "作业管理",
                    iconClass: "fa fa-list",
                    component: () =>
                        import ("@/components/studentManager/WorkManager.vue"),
                },
            ],
        },
        {
            path: "/home",
            name: "数据分析",
            iconClass: "fa fa-bar-chart",
            component: () =>
                import ("@/components/MyHome.vue"),
            children: [{
                    path: "/home/dataOverview",
                    name: "数据概览",
                    iconClass: "fa fa-area-chart",
                    component: () =>
                        import ("@/components/dataAnalyse/DataOverview.vue"),
                },
                {
                    path: "/home/mapOverview",
                    name: "地图概览",
                    iconClass: "fa fa-area-chart",
                    component: () =>
                        import ("@/components/dataAnalyse/MapOverview.vue"),
                },
                {
                    path: "/home/travelMap",
                    name: "旅游地图",
                    iconClass: "fa fa-area-chart",
                    component: () =>
                        import ("@/components/dataAnalyse/TravelMap.vue"),
                },
                {
                    path: "/home/scoreMap",
                    name: "分数地图",
                    iconClass: "fa fa-area-chart",
                    component: () =>
                        import ("@/components/dataAnalyse/ScoreMap.vue"),
                },
            ],
        },
        {
            path: "/home",
            name: "用户中心",
            iconClass: "fa fa-user",
            component: () =>
                import ("@/components/MyHome.vue"),
            children: [{
                path: "/home/userCentre",
                name: "用户管理",
                iconClass: "fa fa-user-circle-o",
                component: () =>
                    import ("@/components/userCentre/User.vue"),
            }, ],
        },
    ],
});

router.beforeEach(async(to, from, next) => {
    if (to.path != "/login") {
        const token = getItem("token");
        if (token) {
            // 向后端请求，查看token是否有效
            const { data: res } = await http.post("/authentication");
            if (res.status === 200) {
                next();
            } else {
                // 清空token
                removeItem("token");
                next("/login");
            }
        } else {
            next("/login");
        }
    } else {
        next();
    }
});

export default router;